package com.huiztech.maystall.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

public class FirstEnterActivity extends Activity {

    // 翻页控件
    private ViewPager mViewPager;

    // 这2个是底部显示当前状态点imageView
    // private ImageView mPage0;
    // private ImageView mPage1;
    private SharedPreferences sp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Context context = FirstEnterActivity.this;
        sp = context.getSharedPreferences("welcome", MODE_PRIVATE);
        Boolean key = sp.getBoolean("BOOLEAN_KEY", false);
        if (key) {
            Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);
            startActivity(intent);
        } else {
            // 去掉标题栏全屏显示
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.welcome);
            mViewPager = (ViewPager) findViewById(R.id.whatsnew_viewpager);

            mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
            // mPage0 = (ImageView) findViewById(R.id.page0);
            // mPage1 = (ImageView) findViewById(R.id.page1);

            /*
             * 这里是每一页要显示的布局，根据应用需要和特点自由设计显示的内容 以及需要显示多少页等
             */
            LayoutInflater mLi = LayoutInflater.from(this);
            View view1 = mLi.inflate(R.layout.welcome_gallery_one, null);
            View view2 = mLi.inflate(R.layout.welcome_gallery_two, null);
            View view3 = mLi.inflate(R.layout.welcome_gallery_three, null);

            /*
             * 这里将每一页显示的view存放到ArrayList集合中 可以在ViewPager适配器中顺序调用展示
             */
            final ArrayList<View> views = new ArrayList<View>();
            views.add(view1);
            views.add(view2);
            views.add(view3);

            /*
             * 每个页面的Title数据存放到ArrayList集合中 可以在ViewPager适配器中调用展示
             */
            final ArrayList<String> titles = new ArrayList<String>();
            titles.add("tab1");
            titles.add("tab2");
            titles.add("tab3");

            // 填充ViewPager的数据适配器
            WelcomeAdapter mPagerAdapter = new WelcomeAdapter(views, titles);
            mViewPager.setAdapter(mPagerAdapter);
        }

    }

    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int page) {

            // 翻页时当前page,改变当前状态圆点图片
            switch (page) {
            case 0:
                // mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                // mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page));
                break;
            case 1:
                // mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
                // mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page));
                break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

    public void startMainActivity(View view) {
        Editor editor = sp.edit();
        editor.putBoolean("BOOLEAN_KEY", true);
        editor.commit();
        Intent intent = new Intent(this, WelcomeActivity.class);
        startActivity(intent);
        FirstEnterActivity.this.finish();
    }
}
